package tp.pr4;

import tp.pr4.Direction;
import tp.pr4.instructions.exceptions.InstructionExecutionException;
import tp.pr4.items.Item;

public class NavigationModule {
	
	private Place actualPlace;
	private Direction actualDirection;
	private City city;
	
 public NavigationModule(City aCity,
            Place initialPlace)
	{
	 actualPlace=initialPlace;
	 city=aCity;
	}
 
 public boolean atSpaceship()
 {
	 return actualPlace.isSpaceship();
 }
 
 public void rotate(Rotation rotation)
 {
	actualDirection=Direction.rotate(actualDirection,rotation);	   
 }
 
 public void move() throws InstructionExecutionException
 {
	if(getHeadingStreet()==null) 
		 throw new InstructionExecutionException ("WALL·E says: There is no street in direction " + getCurrentHeading().toString() + "\n");	
	
	if(!city.lookForStreet(actualPlace, actualDirection).isOpen())		
		throw new InstructionExecutionException ("WALL·E says: Arrggg, there is a street but it is closed!\n");
	
	System.out.println("WALL·E says: Moving in direction " + actualDirection.toString());
    actualPlace=city.lookForStreet(actualPlace, actualDirection).nextPlace(actualPlace);
 }
 
 public Item pickItemFromCurrentPlace(java.lang.String id)
 {
  return actualPlace.pickItem(id);	
 }
 
 public void dropItemAtCurrentPlace(Item it)
 {
	 actualPlace.dropItem(it);
 }
 
 public boolean findItemAtCurrentPlace(java.lang.String id)
 {
	 return actualPlace.existItem(id);
 }
 
 public void initHeading(Direction heading)
 {
	 actualDirection=heading;
 }
 
 public void scanCurrentPlace()
 {
	 System.out.println(actualPlace.toString());
 }
 
 public Street getHeadingStreet()
 {
	 return city.lookForStreet(actualPlace, actualDirection); 
 }
 
 public Direction getCurrentHeading()
 {
	 return actualDirection;
 }
 
 public Place getCurrentPlace()
 {
	 return actualPlace;
 } 

}
